package com.my.zl.console.service.autocode.impl;

import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.zl.common.dto.ReqDTO;
import com.my.zl.common.dto.advancedQuery.PageQueryDTO;
import com.my.zl.common.enums.resp.GlobalEnum;
import com.my.zl.common.exception.BizException;
import com.my.zl.common.utils.CopyUtil;
import com.my.zl.console.common.config.MyselfProperties;
import com.my.zl.console.common.constant.AutocodeC;
import com.my.zl.console.common.constant.AutocodeRespE;
import com.my.zl.console.dao.autocode.ZLCfgGenerateDtlMapper;
import com.my.zl.console.dao.autocode.ZLCfgGenerateMapper;
import com.my.zl.console.dto.autocode.cfgGenerate.ReqCfgGenerateAddDTO;
import com.my.zl.console.dto.autocode.cfgGenerate.ReqCfgGenerateDelDTO;
import com.my.zl.console.dto.autocode.cfgGenerate.ReqCfgGenerateDetailDTO;
import com.my.zl.console.dto.autocode.cfgGenerate.ReqCfgGenerateEditDTO;
import com.my.zl.console.dto.autocode.cfgGenerate.ReqCfgGenerateQueryDTO;
import com.my.zl.console.dto.autocode.cfgGenerate.ReqCfgGenerateStateDTO;
import com.my.zl.console.dto.autocode.cfgGenerate.ResCfgGenerateAddDTO;
import com.my.zl.console.dto.autocode.cfgGenerate.ResCfgGenerateDelDTO;
import com.my.zl.console.dto.autocode.cfgGenerate.ResCfgGenerateDetailDTO;
import com.my.zl.console.dto.autocode.cfgGenerate.ResCfgGenerateEditDTO;
import com.my.zl.console.dto.autocode.cfgGenerate.ResCfgGenerateQueryDTO;
import com.my.zl.console.dto.autocode.cfgGenerate.ResCfgGenerateStateDTO;
import com.my.zl.console.dto.autocode.cfgGenerate.ResCfgGenerateVO2;
import com.my.zl.console.entity.autocode.ZLCfgGenerate;
import com.my.zl.console.po.autocode.cfgGenerate.ZLCfgGenerateVo;
import com.my.zl.console.service.autocode.ICfgGenerateService;
import com.my.zl.console.service.base.BaseManager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 代码生成配置
 * @author ZL
 */
@Service
public class CfgGenerateServiceImpl extends BaseManager implements ICfgGenerateService {

    @Autowired
    private ZLCfgGenerateMapper cfgGenerateMapper;
    @Autowired
    private ZLCfgGenerateDtlMapper cfgGenerateDtlMapper;
    @Autowired
    private MyselfProperties myselfProperties;
    
    @Override
    public ResCfgGenerateQueryDTO queryCfgGenerateList(ReqCfgGenerateQueryDTO reqInfo,PageQueryDTO page) throws Exception{
        // TODO Auto-generated method stub
        ResCfgGenerateQueryDTO resDTO = BeanUtil.copyProperties(page, ResCfgGenerateQueryDTO.class);
        if(StringUtils.isNotBlank(reqInfo.getCfgGenerateTitle())){
            reqInfo.setCfgGenerateTitle("%"+reqInfo.getCfgGenerateTitle()+"%");
        }
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        
        pageHandle(page);//处理分页数据
        //当效率不行时自行计数，或完全自行分页，先查询总数，如果为0不用再次查询了；开启计数：true是,false否
        //临近第一条SQL查询语句自动分页,如果未消费时(即后面没有触发SQL查询)则需主动调用pageHp.clear()清理
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZLCfgGenerateVo> list = cfgGenerateMapper.selectListPage(paramMap);
        resDTO.setData(CopyUtil.copyList(list, ResCfgGenerateVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResCfgGenerateDetailDTO queryCfgGenerateDetail(ReqCfgGenerateDetailDTO reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ResCfgGenerateDetailDTO res = new ResCfgGenerateDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        ZLCfgGenerate view = cfgGenerateMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResCfgGenerateAddDTO insertCfgGenerateInfo(ReqDTO<ReqCfgGenerateAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqCfgGenerateAddDTO reqData = reqInfo.getReqData();
        ResCfgGenerateAddDTO res = new ResCfgGenerateAddDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZLCfgGenerate record = new ZLCfgGenerate();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setCfgGenerateId(IdUtil.simpleUUID());
        record.setDataStatus(AutocodeC.DataStatus.ENABLE.getCode());//状态
        record.setCfgGeneratePath(filePath(myselfProperties.getAutocodePathRoot(),myselfProperties.getAutocodePathMajor(),myselfProperties.getAutocodePathGenerate()
                ,new String[]{record.getCfgGenerateId()}));
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("cfgGenerateId",record.getCfgGenerateId());
        //检查主键是否冲突
        if(cfgGenerateMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        cfgGenerateMapper.insertSelective(record);
        
        ZLCfgGenerate keyInfo = cfgGenerateMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResCfgGenerateEditDTO updateCfgGenerateInfo(ReqDTO<ReqCfgGenerateEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqCfgGenerateEditDTO reqData = reqInfo.getReqData();
        ResCfgGenerateEditDTO res = new ResCfgGenerateEditDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZLCfgGenerate record = new ZLCfgGenerate();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        cfgGenerateMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("cfgGenerateId",record.getCfgGenerateId());
        ZLCfgGenerate keyInfo = cfgGenerateMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResCfgGenerateDelDTO deleteCfgGenerateInfo(ReqDTO<ReqCfgGenerateDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqCfgGenerateDelDTO reqData = reqInfo.getReqData();
        ResCfgGenerateDelDTO res = new ResCfgGenerateDelDTO();
        Instant time = getSystemTime().getUtcTime();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("cfgGenerateId",reqData.getCfgGenerateId());
        if(cfgGenerateDtlMapper.countExistKey(paramMap) > 0){
            throw new BizException(AutocodeRespE.DELETE_NOT_ALLOWED);
        }
        
        paramMap = BeanUtil.beanToMap(reqData);
        cfgGenerateMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResCfgGenerateStateDTO updateCfgGenerateState(ReqDTO<ReqCfgGenerateStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqCfgGenerateStateDTO reqData = reqInfo.getReqData();
        ResCfgGenerateStateDTO res = new ResCfgGenerateStateDTO();
        Instant time = getSystemTime().getUtcTime();
         
        ZLCfgGenerate record = new ZLCfgGenerate();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        cfgGenerateMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("cfgGenerateId",record.getCfgGenerateId());
        ZLCfgGenerate keyInfo = cfgGenerateMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}